package com.study;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

/**
 * @Author: MI
 * @Date: 2021/8/19/23:47
 * @Description: 认证
 */
public class TestAuthenticator {
    public static void main(String[] args) {
        //1 创建安全管理器对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        //2 给安全管理器设置realm
        securityManager.setRealm(new IniRealm("classpath:shiro.ini"));

        //3 安全工具类
        SecurityUtils.setSecurityManager(securityManager);

        //4 关键对象 subject主体
        Subject subject = SecurityUtils.getSubject();

        // 5 创建令牌|IncorrectCredentialsException 密码出错|UnknownAccountException用户不存在
        UsernamePasswordToken token = new UsernamePasswordToken("xiaochens", "123");

        //用户认证
        subject.login(token);
        System.out.println("认证状态" + subject.isAuthenticated());


    }
}
